from django.shortcuts import render

# Create your views here.
from django.views import View

from contents.models import ContentCategory
from goods.models import GoodsChannelGroup
from goods.utils import get_categories


class IndexView(View):
    def get(self, request):
        """
        {
            "1":{
                "channels":[
                    {"id":1, "name":"手机", "url":"http://shouji.jd.com/"},
                    {"id":2, "name":"相机", "url":"http://www.itcast.cn/"}
                ],
                "sub_cats":[
                    {
                        "id":38,
                        "name":"手机通讯",
                        "sub_cats":[
                            {"id":115, "name":"手机"},
                            {"id":116, "name":"游戏手机"}
                        ]
                    },
                    {
                        "id":39,
                        "name":"手机配件",
                        "sub_cats":[
                            {"id":119, "name":"手机壳"},
                            {"id":120, "name":"贴膜"}
                        ]
                    }
                ]
            },
            "2":{
                "channels":[],
                "sub_cats":[]
            }
        }
        """

        data = get_categories()

        # {
        #     'index_lbt':[{
        #         'url':'',
        #         'image':'',
        #         'title':'',
        #
        #     },
        #     {},
        #     {}]
        # }

        # 获取首页广告内容
        contents = {}

        cat_all = ContentCategory.objects.all()

        for cat in cat_all:
            contents[cat.key] = cat.content_set.filter(status=True).order_by('sequence')

        context = {
            'categories': data,
            'contents':contents
        }

        return render(request, 'index.html', context=context)
